<template>
    <el-row :gutter="20">
        <el-col :span="6" :offset="2">
            <el-input v-model="base.model.name" placeholder="供应商名称" clearable maxlength="25" />
        </el-col>
        <el-col :span="6">
            <el-input v-model="base.model.linkman" placeholder="联系人" clearable maxlength="25" />
        </el-col>
        <el-col :span="6">
            <el-input v-model="base.model.phone" placeholder="联系电话" clearable maxlength="25" />
        </el-col>
        <el-col :span="1">
            <el-button type="primary" @click="base.query">查询</el-button>
        </el-col>
        <el-col :span="1" v-if="user().my.pids.includes('40')">
            <el-button type="success" @click="base.openAddDialog">添加</el-button>
        </el-col>
    </el-row>
    <divTable :columnObj="base.column" :tableData="base.tableData" :pageData="base.pageData"
        :handleSizeChange="base.handleSizeChange" :handleCurrentChange="base.handleCurrentChange" />

    <el-dialog v-model="add.dialogVisible" title="添加" width="50%" :show-close="false">
        <el-form :model="add.model" label-width="100px" :rules="rules" ref="addForm">
            <el-form-item label="名称" prop="name">
                <el-input v-model.trim="add.model.name" maxlength="50" />
            </el-form-item>
            <el-form-item label="地址">
                <el-input v-model.trim="add.model.address" maxlength="100" />
            </el-form-item>
            <el-form-item label="网站">
                <el-input v-model.trim="add.model.web" maxlength="50" />
            </el-form-item>
            <el-form-item label="联系人">
                <el-input v-model.trim="add.model.linkman" maxlength="50" />
            </el-form-item>
            <el-form-item label="联系电话">
                <el-input v-model.trim="add.model.phone" maxlength="50" />
            </el-form-item>
            <el-form-item label="微信号">
                <el-input v-model.trim="add.model.wechatID" maxlength="50" />
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model.trim="add.model.email" maxlength="50" />
            </el-form-item>
            <el-form-item label="主营产品概述">
                <el-input v-model="add.model.description" type="textarea" :autosize="{ minRows: 3, maxRows: 9 }"
                    maxlength="300" />
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="add.model.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 9 }"
                    maxlength="300" />
            </el-form-item>
            <el-form-item label="开票种类">
                <el-input v-model.trim="add.model.invoiceType" maxlength="50" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <div style="text-align: center;">
                    <el-button type="primary" @click="add.submit" :disabled="add.submitDisabled">提交</el-button>
                </div>
            </span>
        </template>
    </el-dialog>

    <el-dialog v-model="view.dialogVisible" title="查看" width="50%" :show-close="false">
        <el-form :model="view.model" label-width="100px">
            <el-form-item label="名称" prop="name">
                <el-input v-model.trim="view.model.name" disabled />
            </el-form-item>
            <el-form-item label="地址">
                <el-input v-model.trim="view.model.address" disabled />
            </el-form-item>
            <el-form-item label="网站">
                <el-input v-model.trim="view.model.web" disabled />
            </el-form-item>
            <el-form-item label="联系人">
                <el-input v-model.trim="view.model.linkman" disabled />
            </el-form-item>
            <el-form-item label="联系电话">
                <el-input v-model.trim="view.model.phone" disabled />
            </el-form-item>
            <el-form-item label="微信号">
                <el-input v-model.trim="view.model.wechatID" disabled />
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model.trim="view.model.email" disabled />
            </el-form-item>
            <el-form-item label="主营产品概述">
                <el-input v-model="view.model.description" type="textarea" :autosize=true disabled />
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="view.model.remark" type="textarea" :autosize=true disabled />
            </el-form-item>
            <el-form-item label="开票种类">
                <el-input v-model="view.model.invoiceType" disabled />
            </el-form-item>
        </el-form>
    </el-dialog>

    <el-dialog v-model="edit.dialogVisible" title="编辑" width="50%" :show-close="false">
        <el-form :model="edit.model" label-width="120px" :rules="rules" ref="editForm">
            <el-form-item label="名称" prop="name">
                <el-input v-model.trim="edit.model.name" maxlength="50" />
            </el-form-item>
            <el-form-item label="地址">
                <el-input v-model.trim="edit.model.address" maxlength="100" />
            </el-form-item>
            <el-form-item label="网站">
                <el-input v-model.trim="edit.model.web" maxlength="100" />
            </el-form-item>
            <el-form-item label="联系人">
                <el-input v-model.trim="edit.model.linkman" maxlength="50" />
            </el-form-item>
            <el-form-item label="联系电话">
                <el-input v-model.trim="edit.model.phone" maxlength="50" />
            </el-form-item>
            <el-form-item label="微信号">
                <el-input v-model.trim="edit.model.wechatID" maxlength="50" />
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model.trim="edit.model.email" maxlength="50" />
            </el-form-item>
            <el-form-item label="主营产品概述">
                <el-input v-model="edit.model.description" type="textarea" :autosize="{ minRows: 3, maxRows: 9 }"
                    maxlength="300" />
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="edit.model.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 9 }"
                    maxlength="300" />
            </el-form-item>
            <el-form-item label="开票种类">
                <el-input v-model.trim="edit.model.invoiceType" maxlength="50" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <div style="text-align: center;">
                    <el-button type="primary" @click="edit.submit" :disabled="edit.submitDisabled">提交</el-button>
                </div>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { user } from '@/pinia/modules/user'
import { ref, reactive, onBeforeMount } from 'vue'
import { addSupplier, editSupplier, querySuppliers } from "@/api/supplier"
import { message } from '@/components/divMessage/index'

import divTable from '@/components/divTable/index.vue'

const addForm = ref(null)
const editForm = ref(null)
const rules = reactive({
    name: [
        { required: true, message: '请输入供应商名称', trigger: 'blur' },
    ],
})

const base = reactive({
    model: {
        name: "",
        linkman: "",
        phone: "",
    },
    column: {
        headers: [
            {
                prop: "name",
                label: "名称",
            },
            {
                prop: "web",
                label: "网站",
            },
            {
                prop: "linkman",
                label: "联系人",
            },
            {
                prop: "phone",
                label: "联系电话",
            },
            {
                type: "operation",
                label: "操作",
                operations: [
                    {
                        isShow: (index, row) => {
                            return true
                        },
                        label: "查看",
                        type: "success",
                        align: "center",
                        sortable: false,
                        onClick: (index, row) => base.openViewDialog(index, row)
                    },
                    {
                        isShow: (index, row) => {
                            if (user().my.pids.includes('41')) {
                                return true
                            }
                            return false
                        },
                        label: "编辑",
                        type: "primary",
                        align: "center",
                        sortable: false,
                        onClick: (index, row) => base.openEditDialog(index, row)
                    },
                ]
            },
        ],
    },
    tableData: [],
    pageData: {
        total: 0,
        pageSize: 10,
        pageNo: 1
    },
    query: () => {
        querySuppliers(base.model, base.pageData).then((res) => {
            if (res.status == 1) {
                base.tableData = res.data.data
                base.pageData.total = res.data.total
                base.pageData.pageSize = res.data.pageSize
                base.pageData.pageNo = res.data.pageNo
            } else {
                message("查询失败", "error")
            }
        })
    },
    handleSizeChange: (e) => {
        base.pageData.pageSize = e
        base.pageData.pageNo = 1
        base.query()
    },
    handleCurrentChange: (e) => {
        base.pageData.pageNo = e
        base.query()
    },
    openAddDialog: () => {
        add.dialogVisible = true
    },
    openViewDialog: (index, row) => {
        view.model = row
        view.dialogVisible = true
    },
    openEditDialog: (index, row) => {
        edit.model = row
        edit.dialogVisible = true
    },
})

const add = reactive({
    dialogVisible: false,
    submitDisabled: false,
    model: {
        name: "",
        address: "",
        web: "",
        linkman: "",
        phone: "",
        wechatID: "",
        email: "",
        description: "",
        remark: "",
        invoiceType: "",
    },
    submit: () => {
        addForm.value.validate((valid) => {
            if (valid) {
                add.submitDisabled = true
                addSupplier(add.model).then((res) => {
                    if (res.status == 1) {
                        message("添加成功", "success")
                        base.query()
                    } else {
                        message("添加失败", "error")
                    }
                    add.dialogVisible = false
                    add.model = {
                        name: "",
                        address: "",
                        web: "",
                        linkman: "",
                        phone: "",
                        wechatID: "",
                        email: "",
                        description: "",
                        remark: "",
                    }
                    add.submitDisabled = false
                })
            } else {
                return false;
            }
        })
    }
})

const view = reactive({
    dialogVisible: false,
    model: {
        name: "",
        address: "",
        web: "",
        linkman: "",
        phone: "",
        wechatID: "",
        email: "",
        description: "",
        remark: "",
        invoiceType: "",
    },
})

const edit = reactive({
    dialogVisible: false,
    submitDisabled: false,
    model: {
        id: 0,
        name: "",
        address: "",
        web: "",
        linkman: "",
        phone: "",
        wechatID: "",
        email: "",
        description: "",
        remark: "",
        invoiceType: "",
    },
    submit: () => {
        editForm.value.validate((valid) => {
            if (valid) {
                edit.submitDisabled = true
                editSupplier(edit.model).then((res) => {
                    if (res.status == 1) {
                        message("编辑成功", "success")
                        base.query()
                    } else {
                        message("编辑失败", "error")
                    }
                    edit.dialogVisible = false
                    edit.model = {
                        id: 0,
                        name: "",
                        address: "",
                        web: "",
                        linkman: "",
                        phone: "",
                        wechatID: "",
                        email: "",
                        description: "",
                        remark: "",
                    }
                    edit.submitDisabled = false
                })
            } else {
                return false;
            }
        })
    }
})

onBeforeMount(() => {
    base.query()
})
</script>